/*
 * Hidden Equity LLC
 * Author: Maksym Fedyay, Kyiv, mcsimf@gmail.com
 * Date: 29.02.2012
 */

package com.hiddenequity.adgroup_service;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.hiddenequity.acts.Act_Super;
import com.hiddenequity.utils.Converter;
import com.hiddenequity.utils.XMLUtils;

public class AdGroupResponseParser {

	Document _XMLDocument;
	
	
	/*
	 * Constructor
	 */
	public AdGroupResponseParser(String XMLData){

		try {
			_XMLDocument = XMLUtils.XMLStringToDoc(XMLData);
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
	/*
	 * Check response for errors
	 */
	public String checkForErrors(){
		String error = null;
		try {
	        
	        NodeList faultstring = _XMLDocument.getElementsByTagName("faultstring");
	        error =  XMLUtils.getCharacterDataFromElement((Element) faultstring.item(0));
	        
	    }
	    catch (Exception e) {
	    	e.printStackTrace();
	    }
		return error;
	}//checkForErrors
	
	
	
	/*
	 * Parse campaign list data
	 */
	public ArrayList<HashMap<String, String>> getAdGroupList(){
		ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
		
		try {
	        
			Act_Super.resetTotals();
			
			NodeList nodes = _XMLDocument.getElementsByTagName("entries");
	        
	        for (int i = 0; i < nodes.getLength(); i++) {
                //fill up hush map for list population
            	HashMap<String,String> temp = new HashMap<String,String>();

	        	Element element = (Element) nodes.item(i);

	        	NodeList name = element.getElementsByTagName("name");
	        	temp.put("name", XMLUtils.getCharacterDataFromElement((Element) name.item(0)));

	        	NodeList status = element.getElementsByTagName("status");
	        	temp.put("status", XMLUtils.getCharacterDataFromElement((Element) status.item(0)));
	        	
	        	NodeList clicks = element.getElementsByTagName("clicks");
	        	temp.put("clicks", XMLUtils.getCharacterDataFromElement((Element) clicks.item(0)));
	
	        	NodeList ctr = element.getElementsByTagName("ctr");
	        	temp.put("ctr", Converter.formatStrNumberPercent(
	        			XMLUtils.getCharacterDataFromElement((Element) ctr.item(0))
	        			));
	        	
	        	NodeList cost = element.getElementsByTagName("cost");
	        	Element costElement = (Element) cost.item(0);
	        	NodeList costAmount = costElement.getElementsByTagName("microAmount");
	        	temp.put("cost", Converter.microamountToStr(
	        			XMLUtils.getCharacterDataFromElement((Element) costAmount.item(0))
	        			));
	        	
	        	NodeList avgCpc = element.getElementsByTagName("averageCpc");
	        	Element avgCpcElement = (Element) avgCpc.item(0);
	        	NodeList avgCpcAmount = avgCpcElement.getElementsByTagName("microAmount");
	        	temp.put("avgcpc", Converter.microamountToStr(
	        			XMLUtils.getCharacterDataFromElement((Element) avgCpcAmount.item(0))
	        			));

	        	NodeList id = element.getElementsByTagName("id");
	        	temp.put("id", XMLUtils.getCharacterDataFromElement((Element) id.item(0)));
	        	
	        	NodeList impressions = element.getElementsByTagName("impressions");
	        	temp.put("impressions", XMLUtils.getCharacterDataFromElement((Element) impressions.item(0)));
	        	
	        	Act_Super.uppendStatsToTotal(temp);
	        	
	        	list.add(temp);
	        }
	        
	        Act_Super.calculateTotalStats();

	    } catch (Exception e) {
	    	e.printStackTrace();
	    }

		return list;
	}
	
	
	/*
	 * Parse campaign data, to display in edit form
	 * Only one campaign
	 */
	public Map<String, String> getAdGroupData(){
		Map<String, String> campaignData = new HashMap<String, String>();
		
		try {
      
			NodeList nodes = _XMLDocument.getElementsByTagName("entries");//<--- put valid tag

        	Element element = (Element) nodes.item(0);

        	NodeList name = element.getElementsByTagName("name");
        	campaignData.put("name", XMLUtils.getCharacterDataFromElement((Element) name.item(0)));

        	NodeList adGroupBidsType = element.getElementsByTagName("AdGroupBids.Type");
        	String adGroupBidsTypeData = XMLUtils.getCharacterDataFromElement((Element) adGroupBidsType.item(0));
        	campaignData.put("adGroupBidsType", adGroupBidsTypeData);
        	
        	if(adGroupBidsTypeData.equals("ManualCPCAdGroupBids")){
            	NodeList keywordMaxCpc = element.getElementsByTagName("keywordMaxCpc");
            	Element keywordMaxCpcElement = (Element) keywordMaxCpc.item(0);
            	NodeList budgetAmount = keywordMaxCpcElement.getElementsByTagName("microAmount");
            	campaignData.put("keywordMaxCpc", XMLUtils.getCharacterDataFromElement((Element) budgetAmount.item(0)));

            	NodeList keywordContentMaxCpc = element.getElementsByTagName("keywordContentMaxCpc");
            	Element keywordContentMaxCpcElement = (Element) keywordContentMaxCpc.item(0);
            	NodeList bidLimitAmount = keywordContentMaxCpcElement.getElementsByTagName("microAmount");
            	campaignData.put("keywordContentMaxCpc", XMLUtils.getCharacterDataFromElement((Element) bidLimitAmount.item(0)));        		
        	} else {
            	NodeList keywordMaxCpc = element.getElementsByTagName("proxyKeywordMaxCpc");
            	Element keywordMaxCpcElement = (Element) keywordMaxCpc.item(0);
            	NodeList budgetAmount = keywordMaxCpcElement.getElementsByTagName("microAmount");
            	campaignData.put("keywordMaxCpc", XMLUtils.getCharacterDataFromElement((Element) budgetAmount.item(0)));
        	}
	    }
	    catch (Exception e) {
	    	e.printStackTrace();
	    }

		return campaignData;
	}//getAdGroupData	
	
}
